package com.hospital.service.impl;

import com.hospital.domain.pojo.Ward;
import com.hospital.domain.utils.Result;
import com.hospital.mapper.WardMapper;
import com.hospital.service.WardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WardServiceImpl implements WardService {

    @Autowired
    private WardMapper wardMapper;

    @Override
    public Result<List<Ward>> getAllWards() {
        try {
            List<Ward> wards = wardMapper.selectList(null);
            return Result.ok(wards);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    @Override
    public Result<List<Ward>> getWardsByDepartment(Integer depId) {
        try {
            List<Ward> wards = wardMapper.selectByDepartment(depId);
            return Result.ok(wards);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    @Override
    public Result<List<Ward>> getAvailableWardsByType(String wType) {
        try {
            List<Ward> wards = wardMapper.selectAvailableByType(wType);
            return Result.ok(wards);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    @Override
    public Result<Ward> getWardById(Integer wId) {
        try {
            Ward ward = wardMapper.selectById(wId);
            if (ward != null) {
                return Result.ok(ward);
            } else {
                return Result.error("病房不存在");
            }
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    @Override
    public Result<String> updateWardAvailability(Integer wId, Integer availableBeds) {
        try {
            Ward ward = wardMapper.selectById(wId);
            if (ward == null) {
                return Result.error("病房不存在");
            }
            
            ward.setWAvailableBeds(availableBeds);
            int result = wardMapper.updateById(ward);
            
            if (result > 0) {
                return Result.ok("病房可用床位更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
}
